package com.nineclock.system.controller;

import com.nineclock.api.system.OrganizationControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    private SysCompanyUserService companyUserService;

    @Autowired
    private SysDepartmentService departmentService;

    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> companyUserDTOList = companyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOList);
    }


    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartments() {
        List<SysDepartmentDTO> depList = departmentService.queryDepartments();
        return Result.success(depList);
    }


    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> queryMembers(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "departmentId", defaultValue = "0")Long departmentId) {

        PageResult<SysCompanyUserDTO> pageResult = companyUserService.queryMembers(page, pageSize, keyword, departmentId);
        return Result.success(pageResult);
    }


    @Override
    @PostMapping("/uploadExcel")
    public Result importData(MultipartFile excelFile) throws IOException {
        companyUserService.parseExcel(excelFile);
        return Result.success();
    }


    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> depIds = departmentService.queryDepartmentIds();
        return Result.success(depIds);
    }


    @Override
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = companyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }


    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimleDepartment(@RequestParam(value = "id") Long id,
                                                                        @RequestParam(value = "includeMember", defaultValue = "0") Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> simpleList = departmentService.querySimleDepartment(id, includeMember);
        return Result.success(simpleList);
    }


    @Override
    @GetMapping("/allDepId/{companyUserId}")
    public Result<List<Long>> queryDepartmentIdsByUserId(@PathVariable("companyUserId") Long companyUserId) {
        List<Long> depIds = departmentService.queryDepartmentIdsByUserId(companyUserId);
        return Result.success(depIds);
    }
}
